package com.topnav.mc.position;

import com.alibaba.fastjson2.JSON;
import com.js.lib.cache.SysCache;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.common.Constant;
import com.topnav.mc.uitls.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定信信息
 * @Author: kenny
 * @Date: 2023/10/19 16:12
 */
@Component
public class PositionUtil {
    @Autowired
    private SysCache sysCache;
    /**
     * 取当前定位信息
     * @param deviceCode
     * @return
     */
    public PositionInfo getCurrentPosition(String deviceCode){
        return (PositionInfo) sysCache.hget(Constant.REDIS_DEVICE_CURRENT_LOCATION,deviceCode);
    }

    /**
     * 存当前定位信息
     * @param positionInfo
     */
    public void  setCurrentPosition(PositionInfo positionInfo){
        sysCache.hset(Constant.REDIS_DEVICE_CURRENT_LOCATION,positionInfo.getDeviceCode(),positionInfo);
        /*RedisUtil.hset(Constant.REDIS_DEVICE_CURRENT_LOCATION,positionInfo.getDeviceCode(),positionInfo);*/
    }

    /**
     * 取当前所有设备的定位信息MAP
     * @return
     */
    public Map<String,PositionInfo>  getAllCurrentPositionMap(){

        Map<String,PositionInfo> result = new HashMap<>();
        Map<Object,Object> map = sysCache.hmget(Constant.REDIS_DEVICE_CURRENT_LOCATION);
        for(Map.Entry<Object, Object> item : map.entrySet()){
            result.put(item.getKey().toString(),
                    (PositionInfo)item.getValue());
        }
        return result;

/*        Map<Object, Object> map = RedisUtil.hmget(Constant.REDIS_DEVICE_CURRENT_LOCATION);
        for(Map.Entry<Object, Object> item : map.entrySet()){
            result.put(item.getKey().toString(),
                    JSON.parseObject(item.getValue().toString(), PositionInfo.class));
        }
        return result;*/
    }

    /**
     * 取当前所有设备的定位信息LIST
     * @return
     */
    public List<PositionInfo> getAllCurrentPositionList(){
        List<PositionInfo> result = new ArrayList<>();
        Map<Object, Object> map = sysCache.hmget(Constant.REDIS_DEVICE_CURRENT_LOCATION);
        if (map != null){
            for(Map.Entry<Object, Object> item : map.entrySet()){
                result.add( (PositionInfo)item.getValue());
            }
        }


/*        Map<Object, Object> map = RedisUtil.hmget(Constant.REDIS_DEVICE_CURRENT_LOCATION);
        for(Map.Entry<Object, Object> item : map.entrySet()){
            result.add(JSON.parseObject(item.getValue().toString(), PositionInfo.class));
        }*/
        return result;
    }

}
